using UnityEngine;

public class JoystickCarControll : MonoBehaviour
{
	public static UIJoystick joystick;

	public float steerThreshold = 0.1f;

	public float tankThreshold = 0.85f;

	public static Vector3 joysticPosition
	{
		get
		{
			Vector3 result = Vector3.zero;
			if (joystick != null)
			{
				result = joystick.position;
			}
			return result;
		}
	}

	private void Awake()
	{
		if (joystick == null)
		{
			joystick = GetComponent<UIJoystick>();
		}
	}

	private void OnEnable()
	{
		joystick = GetComponent<UIJoystick>();
	}

	private float SetSteer(float input)
	{
		return Mathf.Clamp01((Mathf.Abs(input) + steerThreshold) * (Mathf.Abs(input) - steerThreshold) * 2f);
	}

	private void Update()
	{
		if (!(joystick != null))
		{
			return;
		}
		if (Mathf.Abs(joystick.position.x) > steerThreshold)
		{
			if (Mathf.Abs(joystick.position.x) > tankThreshold)
			{
				BasicInput.tankTurn = Mathf.Sign(joystick.position.x);
			}
			else
			{
				BasicInput.tankTurn = 0f;
				if (joystick.position.x > 0f)
				{
					BasicInput.JoysticLeftRight = SetSteer(joystick.position.x);
				}
				else
				{
					BasicInput.JoysticLeftRight = 0f - SetSteer(joystick.position.x);
				}
			}
		}
		else
		{
			BasicInput.JoysticLeftRight = 0f;
			BasicInput.tankTurn = 0f;
		}
		BasicInput.LeftRight = BasicInput.JoysticLeftRight;
		if (BasicInput.tankTurn == 0f)
		{
			if (joystick.position.y > 0f)
			{
				BasicInput.Forward = joystick.distance;
				BasicInput.Back = 0f;
			}
			if (joystick.position.y < 0f)
			{
				BasicInput.Back = joystick.distance;
				BasicInput.Forward = 0f;
			}
			if (joystick.position.y == 0f)
			{
				BasicInput.Forward = 0f;
				BasicInput.Back = 0f;
			}
		}
		else
		{
			BasicInput.Forward = 0f;
			BasicInput.Back = 0f;
		}
	}
}
